
#include "poisonfullcoverbullet.h"
#include "resourcemanager.h"
#include "gamemanager.h"
#include "frameanimation.h"
#include "CCSkeletalSprite.h"
#include "gamedatamanager.h"
#include "army.h"
#include "unit.h"
#include "gamescene.h"

#define __super         Bullet

PoisonFullCoverBullet::PoisonFullCoverBullet(
		Army *army1,
		Army *army2,
		EquipmentAttribute *attr,
		GameScene *scene):
__super(attr)
, _gameScene(scene)
, _army1(army1)
, _army2(army2)
, _delay(0.0f) {
}

PoisonFullCoverBullet::~PoisonFullCoverBullet() {
}

void PoisonFullCoverBullet::init() {
	float x = FORMATION_MARGIN + GRID_WIDTH * 1.5f;
	float y = FORMATION_MARGIN;

	ManagedGameNodeInfo info;

	for (int i = 0; i < 11; i++) {
		for (int j = 0; j < 3; j++) {
			FrameAnimation *poison = RM->createAnimation("fx_poison_full", XY_SCALE(x, y));
			poison->setAnchorPoint(ccp(0.5f, 0.0f));
			poison->setLoop(false);

			info.node = poison;
			_gameScene->addOneTimeNode(info);

			y += GRID_HEIGHT * 2;
		}
		x += GRID_WIDTH * 3.0f;
		y = FORMATION_MARGIN;
	}

	AnimationInfo animationInfo = RM->getAnimationInfo("fx_poison_full");
	_delay = animationInfo.frameRate * animationInfo.frames.size();
}

bool PoisonFullCoverBullet::update(float ticks) {
	bool ret = true;
	_delay -= ticks;
	if (_delay <= 0.0f) {
		applyBulletEffect();

		ret = false;
	}

	return ret;
}

void PoisonFullCoverBullet::applyBulletEffect() {
	for (int i = 0; i < TOTAL_LINES; i++) {
			for (Unit *unit :_army1->soldiers[i]) {
				if(unit) {
					unit->applyBullet(this);
				}
			}
			for (Unit *unit :_army2->soldiers[i]) {
				if(unit) {
					unit->applyBullet(this);
				}
			}
		}
}
